package com.gxa.javaserver.task;

import com.gxa.javaserver.service.impl.ProcessDataService;
import com.gxa.javaserver.socket.ServerThread2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author:Teacher黄
 * @date:Created at 2020/06/16
 */
@Component
public class TimerTask {

    @Autowired
    private ProcessDataService processDataService;

    /**
     * 4秒之后执行, 每隔4s还会执行
     */
    @Scheduled(fixedDelay = 4000L)
    public void task01(){
        try {
            // 指定一个端口
            Integer port = 9527;
            // 注册服务端的socket对象, 注册服务
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("socket服务注册成功!");
            // 监听客户端的连接 => 阻塞的状态
            // 如果没有客户端连接, 那么程序就不会向下运行
            System.out.println("等待客户端的连接......");
            while (true){
                // 监听=>阻塞
                Socket accept = serverSocket.accept();
                // 创建并启动线程
                new ServerThread2(accept, processDataService).start();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
